package yajdr.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import yajdr.interfaces.ThreadProgress;
import yajdr.threads.ShadowrunDice;

/**
 * This constructs and controls all elements present in the Shadow Run tab of the main GUI.
 * <p>
 * Copyright: Copyright (c) 2004
 * </p>
 * 
 * @author Andrew Thorburn
 * @version 6.0
 */

public class ShadowrunGui extends JPanel implements ActionListener
{

	private static final long	serialVersionUID	= 7239679204444699805L;

	private JButton				rollDice			= new JButton("Roll Dice");

	private JTextField			numDiceField		= new JTextField("4", 4);

	private JTextField			difField			= new JTextField("4", 4);

	private JTextField			successField		= new JTextField(45);

	private JTextArea			result				= new JTextArea(6, 45);

	private JScrollPane			resultPane			= new JScrollPane(result);

	private JButton[]			presetDice			= new JButton[10];

	private ShadowrunDice[]		dArray;

	private int					finished, successes, botch;

	private String				dieResult;

	private ThreadProgress		parent;



	/**
	 * This adds all of the components to the panel, using seperate loops to ensure that the preset
	 * dice appear over two rows.
	 */
	public ShadowrunGui()
	{
		JPanel fieldPanel = new JPanel();
		fieldPanel.add(new JLabel("Number of Dice to Roll: "));
		fieldPanel.add(numDiceField);
		fieldPanel.add(new JLabel("Difficulty: "));
		fieldPanel.add(difField);
		fieldPanel.add(rollDice);

		JPanel resultPanel = new JPanel();
		resultPanel.add(resultPane);

		JPanel resultPanel2 = new JPanel();
		resultPanel2.add(successField);

		JPanel presetDicePanelOne = new JPanel();
		for (int i = 0; i < presetDice.length / 2; i++)
		{
			presetDice[i] = new JButton("Roll " + (i + 1) + " Dice");
			presetDicePanelOne.add(presetDice[i]);
			presetDice[i].addActionListener(this);
		}

		JPanel presetDicePanelTwo = new JPanel();
		for (int i = (presetDice.length / 2); i < presetDice.length; i++)
		{
			presetDice[i] = new JButton("Roll " + (i + 1) + " Dice");
			presetDicePanelTwo.add(presetDice[i]);
			presetDice[i].addActionListener(this);
		}

		Box entCont = new Box(1);
		entCont.add(fieldPanel);
		entCont.add(resultPanel);
		entCont.add(resultPanel2);
		entCont.add(presetDicePanelOne);
		entCont.add(presetDicePanelTwo);

		add(entCont);

		rollDice.addActionListener(this);
		result.setLineWrap(true);
	}



	public void actionPerformed(ActionEvent ae)
	{
		Object buttonPressed = ae.getSource();

		if (buttonPressed == rollDice)
		{
			rollDice();
		}
		else
		{
			for (int i = 0; i < presetDice.length; i++)
			{
				if (buttonPressed == presetDice[i])
				{
					numDiceField.setText((i + 1) + "");
					rollDice();
					break;
				}
			}
		}
	}



	private void rollDice()
	{
		successes = 0;
		botch = 0;
		dieResult = "";
		parent.update("Rolling Dice...");

		int dif = Integer.parseInt(difField.getText());
		int diceToRoll = Integer.parseInt(numDiceField.getText());
		int newThread = 2000;
		finished = 0;

		if (dif < 2)
		{
			JOptionPane.showMessageDialog(null, "Look, having a difficulty that low is pointless. The difficulty has been set to 2");
			dif = 2;
			difField.setText("2");
		}

		if ((diceToRoll % newThread) == 0)
		{
			dArray = new ShadowrunDice[(diceToRoll / newThread)];
		}
		else
		{
			if (diceToRoll < newThread)
			{
				dArray = new ShadowrunDice[1];
			}
			else
			{
				String intStr = "" + diceToRoll;
				char[] temp = intStr.toCharArray();
				String temp2 = "";
				for (int i = 0; i <= temp.length - 4; i++)
				{
					temp2 += temp[i] + "";
				}
				int tempInt = Integer.parseInt(temp2);
				dArray = new ShadowrunDice[tempInt + 1];
			}
		}

		for (int i = 0; i < dArray.length; i++)
		{
			if ((diceToRoll - newThread) > 0)
			{
				diceToRoll -= newThread;
				dArray[i] = new ShadowrunDice(newThread, dif, this);
				dArray[i].start();
			}
			else if (diceToRoll > 0)
			{
				dArray[i] = new ShadowrunDice(diceToRoll, dif, this);
				dArray[i].start();
			}
		}
	}



	/**
	 * This is called when the dice rolling thread finishes. It will then call showResult, assuming
	 * that all the threads have finished.
	 * 
	 * @param sux
	 *            int Number of successes generated by the thread
	 * @param botch
	 *            int Number of ones generated by the thread
	 * @param dieResult
	 *            String The actual die rolls generated by the thread
	 */
	public synchronized void finished(int sux, int botch, String dieResult)
	{
		successes += sux;
		this.botch += botch;
		this.dieResult += dieResult;
		finished++;
		parent.update(finished, dArray.length);
		if (finished == dArray.length)
		{
			showResult();
			parent.update("Done");
			parent.resetProgressBar();
		}
	}



	public void setParent(ThreadProgress tp)
	{
		parent = tp;
	}



	/**
	 * Only called when all threads have finished executing. It will then set the output to whatever
	 * the dice have determined.
	 */
	private void showResult()
	{
		if (successes > 0)
		{
			result.setText("You rolled: " + "\n" + dieResult + "\n" + "at difficulty " + difField.getText());
			successField.setText("You got " + successes + " successes at difficulty " + difField.getText());
		}
		else if (botch == Integer.parseInt(numDiceField.getText()))
		{
			result.setText("You rolled: " + "\n" + dieResult + "\n" + "at difficulty " + difField.getText());
			successField.setText("You botched, managing to roll all ones.");
		}
		else
		{
			result.setText("You rolled: " + "\n" + dieResult + "\n" + "at difficulty " + difField.getText());
			successField.setText("You failed");
		}
	}
}
